/*
 *  Emotion Media Center
 *
 *  Copyright (C) 2009 Davide Andreoli <dave@gurumeditation.it>
 *
 *  License LGPL-2.1, see COPYING file at project folder.
 *
 */

#include "emc.h"
#include "emc_module.h"
#include "emc_mainmenu.h"
#include "emc_config.h"
#include "emc_browser_obj.h"
#include "emc_gui.h"
#include "emc_videos.h"

#define EMC_MODULE_NAME "video_film"

#define LOCAL_DEBUG 1
#if LOCAL_DEBUG
#define D DBG
#else
#define D(...)
#endif


/*****************************************************************************/
/*                         internals                                         */
/*****************************************************************************/

static void
_film_populate(Evas_Object *browser, const char *path)
{
   Eina_List *files, *list = NULL;
   Emc_Browser_Item *item;
   char buf[PATH_MAX];
   char *f;

   D(" ");

   files = ecore_file_ls(path);
   EINA_LIST_FREE(files, f)
   {
      snprintf(buf, sizeof(buf), "%s/%s", path, f);
      item = emc_browser_item_new(buf, f, NULL, ecore_file_is_dir(buf));
      list = eina_list_append(list, item);
      free(f);
   }

   emc_browser_obj_push(browser, list, EINA_TRUE);
}

static void
_browser_selected_cb(void *data, Evas_Object *obj, void *event_info)
{
   Emc_Browser_Item *item = event_info;

   D("SEL!! %s", item->label);
   if (item->is_dir)
   {
      _film_populate(obj, item->uri);
   }
   else
   {
      emc_video_play(item->uri);
   }
}

static void
_film_show_root(void *data)
{
   Evas_Object *brow;
   const char *dir;
   char buf[PATH_MAX];

   brow = emc_browser_obj_add(evas_object_evas_get(emc->layout));
   evas_object_smart_callback_add(brow, "selected", _browser_selected_cb, NULL);

   dir = emc_config_string_get("video", "film_dir");
   if (!dir)
   {
      snprintf(buf, sizeof(buf), "%s/Video/Film", getenv("HOME"));
      emc_config_string_set("video", "film_dir", buf);
      dir = emc_config_string_get("video", "film_dir");
   }
   _film_populate(brow, dir);

   emc_gui_content_show(brow);
}

/******************************************************************************/
/*                     Module interface                                       */
/******************************************************************************/

Eina_Bool
module_init(Emc_Module *em)
{
   Evas_Object *ic;
   
   D(" ");

   ic = elm_icon_add(emc->win);
   elm_icon_file_set(ic, emc->theme_file, "icon/video");
   emc_mainmenu_item_append("videos_film", "Films", ic, "videos", 1,
                            _film_show_root, NULL);

   return EINA_TRUE;
}

Eina_Bool
module_shutdown(Emc_Module *em)
{
   D(" ");

   emc_mainmenu_item_del("videos_film");
   return EINA_TRUE;
}

Emc_Module_Api module_api =
{
   EMC_MODULE_API_VERSION,
   EMC_MODULE_NAME,
   "Film",
   NULL,
   "Module to play Films",
   "bla bla bla<br><b>bla bla bla</b><br><br>bla.",
   {
      module_init,
      module_shutdown
   }
};
